
console.log("111111111111",localStorage.getItem("allSelected"))
const cartModule = {
  namespaced:true,
  state: {
    cartList:JSON.parse(localStorage.getItem("cart")) || [],
    allSelected:JSON.parse(localStorage.getItem("allSelected")) || false,
    // totalCount:0,
    // totalPrice:0
  },
  mutations: {
    ADD_STORE_TO_CART(state,item){
      //找到重复的商品
      var flag = false;
      state.cartList.forEach(element => {
        if(element.id === item.id){
          flag = true;
          element.count++;
        }
      });

      if(!flag){
        item.count = 1;
        item.checked = false;
        state.cartList.push(item);
      }
      // console.log(state.cartList)
      localStorage.setItem("cart",JSON.stringify(state.cartList));
    },
    CHANGE_STORE_CHECKED(state,item){
      var tempItem = state.cartList.find(element => {
        return element.id === item.id;
      })

      if(tempItem){
        tempItem.checked = item.checked;
      }

      state.allSelected  = state.cartList.every(element => {
        return  element.checked === true;
      });  

      localStorage.setItem("allSelected",JSON.stringify(state.allSelected))
      localStorage.setItem("cart",JSON.stringify(state.cartList));
      
      // state.totalPrice = 0;
      // state.totalCount = 0;
      // state.cartList.forEach(element => {
      //   if(element.checked){
      //     state.totalCount+=element.count;
      //     state.totalPrice+=element.count*element.actualPrice
      //   }
      // });
      
    },
    CHANGE_ALL_SELECTED(state,boolean){
      state.allSelected = boolean;
      state.cartList.forEach(element => {
        element.checked = boolean;
      })
      localStorage.setItem("allSelected",JSON.stringify(state.allSelected))
      localStorage.setItem("cart",JSON.stringify(state.cartList));

      // state.totalPrice = 0;
      // state.totalCount = 0;
      // state.cartList.forEach(element => {
      //   if(element.checked){
      //     state.totalCount+=element.count;
      //     state.totalPrice+=element.count*element.actualPrice
      //   }
      // });
    },
    CHANGE_ITEM_COUNT(state,item){
      console.log(item)
      state.cartList.forEach(element => {
        if(element.id === item.id){
          element.count = item.count
        }
      });

      localStorage.setItem("allSelected",JSON.stringify(state.allSelected))
      localStorage.setItem("cart",JSON.stringify(state.cartList));
    },
    DELETE_ITEM(state,id){
      var index = state.cartList.findIndex(element =>{
        return element.id  === id;
      })

      if(index!=-1){
        state.cartList.splice(index,1);
      }
      localStorage.setItem("allSelected",JSON.stringify(state.allSelected))
      localStorage.setItem("cart",JSON.stringify(state.cartList));
    }
  },
  getters:{
    totalCount:state => {
      var  tempCount = 0;
      state.cartList.forEach(element => {
        if(element.checked){
          tempCount+=element.count;
        }
      });
      return tempCount;
    },
    totalPrice:state => {
      var tempPrice = 0;
      state.cartList.forEach(element => {
        if(element.checked){
          tempPrice+=element.count*element.actualPrice;
        }
      });
      return tempPrice;
    }
  },
  actions: {
    
  },
  modules: {
    
  }

}

export default cartModule;